﻿<?php

/*
Socket class 0.0.1 alpha
Paweł 'Pavlus' Janisio
Under GNU GPLv3
*/

class Network
{

private $stream = NULL;
private $streamStatus = array();

	public function connect ($host, $port, $timeout)
	{
	$this->host = $host;
	$this->port = $port;
	$this->timeout = $timeout;
	
	//get ip address 
	$this->hostIp = gethostbyname($this->host);
	
		//lets try to connect
		$this->stream = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
		
			if(!$this->stream)
			{
				echo 'Socket error: '.$errstr.' ('.$errno.')';
			}
			else
				return TRUE;
			
			}
			
			/*
			You dont have to unblock the stream, it may cause problems while transferring data
			0 - unblocked, 1-blocked (default)
			*/
	public function setStreamUnblocked() 
			{
			
				if(this->stream)
				{
				strim_set_blocking(this->stream, 0);
				
				}
			
			}
			
			
	public function getStreamStatus($display = NULL, $stream = NULL)
		{
		
		if(isset($stream))
		$this->stream = $stream;
		
		$this->streamStatus = @socket_get_status($this->stream);
			if($this->streamStatus['timed_out'] == TRUE)
			{
				if($display)
			echo 'Connection timed out'.$this->timout.' seconds';
			}
			else if($this->streamStatus['blocked'] == TRUE)
			{
				if($display)
			echo 'Stream in blocked mode ('.$this->host.')';
			}
			else if($this->streamStatus['eof'] == TRUE)
			{
				if($display)
			echo 'Connection returned EOF';
			}
		
		}
		
	public function __destruct()
		{
			if($this->stream)
				fclose($this->stream);
		
		}



}
	

?>